import { request } from '../../utils/util'
let app = getApp()

Page({
  data: {
    bannerData: [],
    swiperIdx: 0,
    top: app.globalData.rightBarInfo.top,
    height: app.globalData.rightBarInfo.height,
    list: [],
    page: 1,
    loadMore: true,
  },

  // 轮播特效果二
  bindchange(e) {
    this.setData({
      swiperIdx: e.detail.current
    })
  },
  // 获取banner数据
  getBanner() {
    request({
      url: '/api/web/slideshow',
      success: ({ code, data }) => {
        if (code === 200) {
          this.setData({
            bannerData: data
          })
        }
      }
    })
  },
  onClickBanner(ev) {
    let { item } = ev.currentTarget.dataset;
    if (item.link) {
      wx.navigateTo({
        url: `/${item.link.replace('.html', '')}`
      })
    }
  },
  // 获取列表数据
  getList(page, latitude, longitude) {
    wx.showLoading({
      title: '加载中..',
      mask: true
    })
    request({
      url: '/api/web/venue/index',
      params: {
        page,
        latitude,
        longitude
      },
      success: ({ code, data, message }) => {
        wx.hideLoading()
        if (code === 200) {
          let currentData = data.data.map(item => {
            item.open_time_show = item.open_time.substr(0, 5)
            item.close_time_show = item.close_time.substr(0, 5)
            return item
          })
          if (data.current_page === 1) {
            this.data.list = []
          }
          this.setData({
            list: [
                ...this.data.list,
                ...currentData
            ],
            page: data.current_page,
            loadMore: data.current_page !== data.last_page
          })
        } else {
          wx.showToast({
            title: message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  onLoad () {
    wx.getLocation({
      type: 'wgs84',
      success: ({ latitude, longitude }) => {
        this.getList(1, latitude, longitude);
      }
    })
    this.getBanner();
    this.getList();
  },
  onReachBottom () {
    if (!this.data.loadMore) {
      return false
    }
    this.getList(this.data.page + 1)
  },
  onShareAppMessage () {

  },
})
